package com.weme.common.socket.base

import android.util.Log
import com.weme.common.buff.IoBuffer
import org.java_websocket.client.WebSocketClient
import org.java_websocket.drafts.Draft_6455
import org.java_websocket.handshake.ServerHandshake
import java.net.URI
import java.nio.ByteBuffer
import kotlin.Exception

/**
 * 一个增加了很多功能自定义WebSocketClient
 *
 * @author 
 */
class HandlerWebSocketClient(uri: URI) :
    WebSocketClient(uri, Draft_6455()), DecoderCoderOutput, EncoderCoderOutput {

    private var handler: HandlerAdapter? = null

    private var codecFactory: CoderFactory? = null

    init {
        // 禁用心跳丢失检测
        connectionLostTimeout = 0
    }
    /**
     * 设置消息的处理器
     */
    fun setHandler(handler: HandlerAdapter) {
        this.handler = handler
    }

    /**
     * 设置编码/解码器
     */
    fun setCodecFactory(codecFactory: CoderFactory) {
        this.codecFactory = codecFactory
    }

    override fun onOpen(handshakedata: ServerHandshake?) {
        handler?.onOpen(handshakedata)
    }

    override fun onMessage(message: String?) = Unit

    override fun onMessage(bytes: ByteBuffer?) {
        val decoder = codecFactory?.getDecoder(this)
        if (decoder != null) {
            decoder.decode(this, bytes, this)
        } else {
            handler?.onMessage(bytes)
        }
    }

    override fun write(msg: Any) {
        handler?.onMessage(msg)
    }

    override fun write(msg: ByteBuffer) {
        try{
            super.send(msg)
        }catch (e :Exception){}

    }

    override fun onClose(code: Int, reason: String?, remote: Boolean) {
        handler?.onClose(code, reason, remote)
    }

    override fun onError(ex: Exception?) {
        handler?.onError(ex)
    }
    var cmdId = 0
    var seq = 0L

    fun send(msg: Any?,cmdId: Int,seq:Long) {
        this.cmdId = cmdId
        this.seq = seq
        when (msg) {
            is ByteBuffer -> {
                send(msg)
            }
            is ByteArray -> {
                send(msg)
            }
            is String -> {
                send(msg)
            }
            is IoBuffer -> {
                send(msg.buf())
            }
            else -> {
                codecFactory?.getEncoder(this)?.encode(this, cmdId = cmdId,seq, msg, this)
            }
        }
    }

    override fun send(text: String?) {
        val encoder = codecFactory?.getEncoder(this)
        if (encoder != null) {
            encoder.encode(this, cmdId = cmdId,seq, text, this)
        } else {
            super.send(text)
        }
    }

    override fun send(data: ByteArray?) {
        val encoder = codecFactory?.getEncoder(this)
        if (encoder != null) {
            encoder.encode(this,cmdId, seq,data, this)
        } else {
            super.send(data)
        }
    }

    override fun send(bytes: ByteBuffer?) {
        val encoder = codecFactory?.getEncoder(this)
        if (encoder != null) {
            encoder.encode(this, cmdId,seq,bytes,this)
        } else {
            super.send(bytes)
        }
    }
}
